

library(tidyverse)
library(rio)
library(cregg)
library(patchwork)

# set replication folder as working directory
setwd("~replication")

load("data_genderedcost_long.rdata")

# only include completed answers - and answers given before deadline
# 2021-12-20 21:49:52 was the last response within the time frame
df_long <- df_long %>% 
  filter(SurveyStatus==2)

df_long <- df_long %>% 
  filter(SurveyEndTime<="2021-12-20 21:49:52")

##### FIGURE 3 ######
## MARGINAL MEANS FOR SUBSETS OF MEN AND WOMEN
# LEFT HAND-SIDE OF FIGURE 3

df_men <- df_long %>% 
  dplyr::filter(sex=="Man")

mm_men <- cregg::mm(df_men, # data
                    choice~position + remuneration + workload + work_environment, # formula
                    id = id)

mm_men$sex <- "Man" # add sex variable

df_women <- df_long %>% 
  dplyr::filter(sex=="Woman")

mm_women <- cregg::mm(df_women, # data
                      choice~position + remuneration + workload + work_environment, # formula
                      id = id)

mm_women$sex <- "Woman" # add sex variable

#combine estimates from the two subsets, rename to plurals and rearrange levels of feature factor
mm_sex_subsets <- bind_rows(mm_men, mm_women) %>%
  mutate(sex = factor(ifelse(sex=="Man","Men","Women"))) %>% 
  mutate(feature = case_when(feature=="work_environment"~"Working Environment",
                             feature=="workload"~"Workload",
                             feature=="remuneration"~"Remuneration",
                             feature=="position"~"Position")) %>% 
  mutate(feature = factor(feature, levels = c("Position", "Remuneration", "Workload", "Working Environment")))



## PLOT MARGINAL MEANS -- LEFT HAND-SIDE OF FIGURE 3
mm_sex_subsets_plot <- mm_sex_subsets %>% 
  ggplot(data=., aes(y = level, x = estimate, color = sex, shape = sex)) +
  geom_point(aes(shape = sex),
             position = position_dodge2(width = 0.5)) +
  geom_linerange(aes(xmin=estimate-std.error*1.39,
                     xmax=estimate+std.error*1.39),
                 position = position_dodge2(width = 0.5)) +
  xlab("Marginal mean") +
  ylab("") +
  scale_x_continuous(labels = seq(0.3,0.8,0.05), breaks = seq(0.3,0.8,0.05)) +
  geom_vline(xintercept = 0.5, linetype = "dashed") +
  theme_bw() +
  scale_color_grey("", start = 0.1, end = 0.65) +
  scale_shape_discrete("") +
  facet_wrap(~feature, scales = "free_y", ncol = 1) +
  theme(legend.position = "top", panel.background = element_rect(fill = "white"),
        strip.background = element_rect("white"),
        strip.text = element_text(hjust = 0, face = "bold"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank())


###### DIFFERENCE IN MARGINAL MEANS BETWEEN MEN AND WOMEN
## RIGHT HAND-SIDE IN FIGURE 3

mm_sex_differences <- cregg::mm_diffs(df_long, # data
                                      choice~position + remuneration + workload + work_environment, # formula
                                      ~sex)

mm_sex_differences <- mm_sex_differences %>% 
  mutate(feature = case_when(feature=="work_environment"~"Working Environment",
                             feature=="workload"~"Workload",
                             feature=="remuneration"~"Remuneration",
                             feature=="position"~"Position")) %>% 
  mutate(feature = factor(feature, levels = c("Position", "Remuneration", "Workload", "Working Environment")))

mm_sex_differences_plot <- mm_sex_differences %>% 
  ggplot(data=., aes(y = level, x = estimate)) +
  geom_point(shape = 16, color = "black") +
  geom_linerange(aes(xmin=lower,
                     xmax=upper),
                 color = "black") +
  xlab("Differences\n(women - men)") +
  ylab("") +
  scale_x_continuous(labels = seq(-0.2,0.2,0.05), breaks = seq(-0.2,0.2,0.05)) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  theme_bw() +
  scale_shape_discrete("") +
  facet_wrap(~feature, scales = "free_y", ncol = 1) +
  coord_cartesian(xlim = c(-0.1,0.1)) +
  theme(legend.position = "top", panel.background = element_rect(fill = "white"),
        strip.background = element_rect("white"),
        strip.text = element_text(hjust = 0, face = "bold"),
        axis.text.y=element_blank(), axis.ticks.y=element_blank(),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank())


### plot together
mm_sex_subsets_plot + mm_sex_differences_plot + plot_layout(widths = c(3, 1))

ggsave("figure3.pdf", height = 5, width = 10)
ggsave("figure3.png", height = 5, width = 10, dpi = 600)
